﻿var isIE = !!window.ActiveXObject;  
var isIE6 = isIE && !window.XMLHttpRequest; 
/**
 * 任务队列，使用先进后出策略
 * option = {
 *		thread : 1, //线程数
 *	 }
 */ 
function TaskQueue(option) {
    var taskList = [];
	
	if(option==null){
		option = {thread : 10};
	}else if(option.thread == null){
		option.thread = 10;
	}
	
	if(option.interval == null){
		option.interval = 400;
	}
	
	option.runingThread = 0;
    
    this.addTask = function (task) {
		if(task.validate==null){
			task.validate=function(){return true;}
		}
        taskList.push(task);
    };
	
	this.getTaskListSize = function(){return taskList.length;};

    function run(task) {
        if (!task.validate()) {
			--option.runingThread;
            return;
        }
		
		if(task.begin) task.begin();
		task.run(function(){
			endWrap(task);		  
		}); 
    }
	
	function endWrap(task) {
		if(task.callback) task.callback.apply(task,arguments);
		--option.runingThread;
	}
	
	setInterval(function () {
		while (taskList.length > 0 && option.runingThread < option.thread) {
			++option.runingThread;
			run(taskList.pop());
		}
	}, option.interval)
}


/**
 * 哈希表
 */
function HashMap(){
	 /** Map 大小 **/
	 var size = 0;
	 /** 对象 **/
	 var entry = new Object();
	  
	 /** 存 **/
	 this.put = function (key , value)
	 {
		 if(!this.containsKey(key))
		 {
			 size ++ ;
		 }
		 entry[key] = value;
	 };
	 
	 this.set = function(key,value){
		this.put(key,value);	 
	};
	  
	 /** 取 **/
	 this.get = function (key)
	 {
		 if( this.containsKey(key) )
		 {
			 return entry[key];
		 }
		 else
		 {
			 return null;
		 }
	 };
	  
	 /** 删除 **/
	 this.remove = function ( key )
	 {
		 if( delete entry[key] )
		 {
			 size --;
		 }
	 };
	  
	 /** 是否包含 Key **/
	 this.containsKey = function ( key )
	 {
		 return (key in entry);
	 };
	  
	 /** 是否包含 Value **/
	 this.containsValue = function ( value )
	 {
		 for(var prop in entry)
		 {
			 if(entry[prop] == value)
			 {
				 return true;
			 }
		 }
		 return false;
	 };
	  
	 /** 所有 Value **/
	 this.values = function ()
	 {
		 var values = new Array(size);
		 for(var prop in entry)
		 {
			 values.push(entry[prop]);
		 }
		 return values;
	 };
	  
	 /** 所有 Key **/
	 this.keys = function ()
	 {
		 var keys = new Array(size);
		 for(var prop in entry)
		 {
			 keys.push(prop);
		 }
		 return keys;
	 };
	  
	 /** Map Size **/
	 this.size = function ()
	 {
		 return size;
	 };
 }
 
var __JSON_STR_TMP = null;
function getJSON(str){
	eval("__JSON_STR_TMP=("+str+")");
	return __JSON_STR_TMP;
}
 
function copyYuState(obj){
	var newObj = {};
	for(var prop in obj){
		newObj[prop] = obj[prop];
	}
	return newObj;
}
